//  vk.com  http://vk.com/video-26447647_161643797  - 
var
   linkv,v,video,pat, vtag,hd,mail,pass,sWebPage,flv, vkid, uid,hd1,videotype,gsHeaders,nser,nd,cache_file,s :string;
   var Hour, Min, Sec, MSec: Word;
    v1,v2,v3:variant;
  //
  sHtml, sFormatMap, sVideoID, sFormat, itag, sig,sLink:String;
  i, iHeight, iPriority, iMinPriority:Integer ;
  aGUrl:array[0..2] of string;   
begin

   Delete(mpFilePath, 1, 13); 
   v:= HmsSendRequest('vk.com', mpFilePath, 'GET', '', 'http://vk.com', '', 80);
HmsStringToFile(v, SpecialFolderPath(0)+'\v.txt');    
   v :=HmsRemoveLineBreaks(v);
   v:= ReplaceStr(v, '\', '');
   HmsRegExMatch('ajax.preload.*?"<div class=(.*?)>', v,videotype);
s:=s+'videotype= '+videotype+#13 ;   
//    
if videotype='"video_box"' then begin
   HmsRegExMatch('ajax.preload.*?<img src="http://(.*?)/', v,linkv);
s:=s+'linkv= '+linkv+#13 ;     
   hd1:=RightCopy(mpTitle, 3);
s:=s+'hd1= '+hd1+#13 ;   
   if hd1='240' then hd:='0';
   if hd1='360' then hd:='1';
   if hd1='480' then hd:='2';
   if hd1='720' then hd:='3';
   HmsRegExMatch('"vtag":"(.*?)"', v,vtag);
s:=s+'vtag= '+vtag+#13 ;    
   HmsRegExMatch('"no_flv":(.*?),',v,flv);
s:=s+'flv= '+flv+#13 ;   
   HmsRegExMatch('"uid":"(.*?)"', v, uid);
s:=s+'uid= '+uid+#13 ;   
   HmsRegExMatch('"vkid":"(.*?)"', v, vkid);
s:=s+'vkid= '+vkid+#13 ;  
   HmsRegExMatch('"cache_base_'+hd+'":"(.*?)/', v,nser);
s:=s+'nser= '+nser+#13 ;  
   HmsRegExMatch('"cache_base_'+hd+'":".*?/(.*?)"', v,nd);
s:=s+'nd= '+nd+#13 ;   
   HmsRegExMatch('"cache_file_'+hd+'":"(.*?)"', v,cache_file);
s:=s+'cache_file= '+cache_file+#13 ;   
   if nser <>'' then begin
    if (flv='0')and (hd='0')then  video := 'http://cs' +nser+'.vk.com/d'+nd+'/'+cache_file+ '.flv'  
    else  video:='http://cs' +nser+'.vk.com/d'+nd+'/'+cache_file+'.'+hd1+'.mp4' 
   end
   else begin
    if (flv='0')and (hd='0')then  video := 'http://'+linkv+'/assets/videos/'+ vtag+ vkid + '.vk.flv'  
    else  video:='http://'+linkv+'/u'+uid+'/videos/'+vtag+'.'+hd1+'.mp4' 
   end 
end








//
else if Pos ('youtube',v)>0 then begin
//    Youtube - PascalScript
   iMinPriority:=99;
   HmsRegExMatch('ajax.preload.*?embed/(.*?)\?', v, sVideoID);
    //    stream_map
   aGUrl[0] := 'http://www.youtube.com/get_video_info?video_id=';
   aGUrl[1] := 'http://www.youtube.com/watch?has_verified=1&v=';
   // aGUrl[2] := 'http://www.youtube.com/api_video_info?video_id=';
  sHtml := HmsDownloadURL(aGUrl[1]+sVideoID);
   HmsRegExMatch('fmt_stream_map=(.*?)&', sHtml, sFormatMap);
   if sFormatMap = '' then HmsRegExMatch('fmt_url_map=(.*?)&', sHtml, sFormatMap); 
   //     -
   if sFormatMap = '' then begin
          sHtml := HmsDownloadURL(aGUrl[0]+sVideoID); 
          HmsRegExMatch('fmt_stream_map=(.*?)&', sHtml, sFormatMap);
          if sFormatMap = '' then HmsRegExMatch('fmt_url_map=(.*?)&', sHtml, sFormatMap);
   end;
   if  sFormatMap <> '' then  begin
       sFormatMap := HmsHttpDecode(sFormatMap);
       for i := 1 to WordCount(sFormatMap, ',')  do  begin
          sFormat := ExtractWord(i, sFormatMap, ',');
          HmsRegExMatch('itag=(\\d+)', sFormat, itag );
          HmsRegExMatch('url=(.*?)&',  sFormat, sLink);
          sLink := HmsHttpDecode(sLink);
          if (HmsRegExMatch('sig=(.*?)&', sFormat, sig)) then sLink := sLink + '&signature=' + sig;
          if (mpPodcastMediaFormats = '') then begin
             video := sLink;
             break;
          end;
          iHeight:=0;
          if      (IsWordPresent(itag, '13,17',           ',')) then iHeight:=144 
          else if (IsWordPresent(itag, '5,83',            ',')) then  iHeight:=240
          else if (IsWordPresent(itag, '6',               ',')) then  iHeight:=270
          else if (IsWordPresent(itag, '18,34,43,82,100', ',')) then  iHeight:=360
          else if (IsWordPresent(itag, '35,44,101',       ',')) then  iHeight:=480
          else if (IsWordPresent(itag, '22,45,84,102',    ',')) then  iHeight:=720
          else if (IsWordPresent(itag, '37,46',           ',')) then  iHeight:=1080
          else if (IsWordPresent(itag, '38',              ',')) then  iHeight:=3072;
          iPriority := HmsMediaFormatPriority(iHeight, mpPodcastMediaFormats);
          if ((iPriority>=0)and(iPriority<iMinPriority)) then begin
             iMinPriority:=iPriority; 
             video:=sLink;
          end;   
       end;
   end
   else begin
      //   stream_map,      API
        sHtml := HmsDownloadURL('http://gdata.youtube.com/feeds/api/videos/'+sVideoID+'?v=2');
        HmsRegExMatch('<media:content[^>]+url=''([^>]+)''[^>]+type=''video[^>]+yt:format=''6', sHtml, video);
   end;
   if (video = '') then HmsLogMessage(2, mpTitle+':   youtube.com  ('+sLink+')');
    
end;



   v2 := CreateOleObject('SAPI.SpVoice');
   v2.Speak('s''sylkaa p''ushla');
if  (video = '')then begin
   video := 'C:\Program Files\Home Media Server\Presentation\Images\videook.mp4' ;
   v2.Speak('oshibkaa');
end;
   MediaResourceLink := video;
HmsLogMessage(3, 'video= '+video) ;
s:=s+'video= '+video+#13 ;
HmsStringToFile(s, SpecialFolderPath(0)+'\s.txt');    
//ShowMessage(video);
end.